package com.google.code.swingonfire.component;

import java.awt.AlphaComposite;
import java.awt.Component;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.LayoutManager;

import javax.swing.JPanel;

import com.google.code.swingonfire.layout.FillLayout;

public class JTransparentPanel extends JPanel
{

    private static final long serialVersionUID = 6040632068584047975L;

    private Container contentPane = null;
    private final double transparency = 0.5;

    public JTransparentPanel()
    {
        super(new FillLayout());

        setOpaque(false);
        setContentPane(new JPanel());
    }

    public JTransparentPanel(boolean isDoubleBuffered)
    {
        super(new FillLayout(), isDoubleBuffered);

        setOpaque(false);
        setContentPane(new JPanel(isDoubleBuffered));
    }

    public JTransparentPanel(LayoutManager layout, boolean isDoubleBuffered)
    {
        super(new FillLayout(), isDoubleBuffered);

        setOpaque(false);
        setContentPane(new JPanel(layout, isDoubleBuffered));
    }

    public JTransparentPanel(LayoutManager layout)
    {
        super(new FillLayout());

        setOpaque(false);
        setContentPane(new JPanel(layout));
    }

    public JTransparentPanel(Container contentPane)
    {
        super(new FillLayout());

        setOpaque(false);
        setContentPane(contentPane);
    }

    public Component getContentPane()
    {
        return contentPane;
    }

    public void setContentPane(Container contentPane)
    {
        if (this.contentPane != contentPane)
        {
            if (this.contentPane != null)
            {
                Container current = this.contentPane;

                this.contentPane = null;
                remove(current);
            }

            if (contentPane != null)
            {
                add(contentPane);
            }

            this.contentPane = contentPane;
        }
    }

    @Override
    protected void addImpl(Component comp, Object constraints, int index)
    {
        if (contentPane != null)
        {
            contentPane.add(comp, constraints, index);
        }
        else
        {
            super.addImpl(comp, constraints, index);
        }
    }

    @Override
    public void remove(Component comp)
    {
        if (contentPane != null)
        {
            contentPane.remove(comp);
        }
        else
        {
            super.remove(comp);
        }
    }

    @Override
    public void setLayout(LayoutManager manager)
    {
        if (contentPane != null)
        {
            contentPane.setLayout(manager);
        }
        else
        {
            super.setLayout(manager);
        }
    }

    @Override
    public void paint(Graphics g)
    {
        Graphics2D g2d = (Graphics2D) g.create();

        if (transparency != 0.0)
        {
            g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER, (float) transparency));
        }

        super.paint(g2d);
    }

}
